package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Outbound;
import com.igeek.collateral.mapper.InboundMapper;
import com.igeek.collateral.mapper.OutboundMapper;
import com.igeek.collateral.service.IOutboundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.vo.PageQueryVO;
import com.igeek.collateral.vo.ZOutboundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 出库记录 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class OutboundServiceImpl extends ServiceImpl<OutboundMapper, Outbound> implements IOutboundService {


    @Autowired
    private OutboundMapper outboundMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    //已出库的分页查询
    @Transactional(readOnly = true)
    @Override
    public Page<Outbound> findPageOut(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Outbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据出库编号)
        LambdaQueryWrapper<Outbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Outbound::getStatus,1)
                .eq(StringUtils.hasLength(queryString),Outbound::getNum,queryString);
        //执行分页查询条件
        Page<Outbound> pageO = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageO;
    }


    //添加出库信息
    @Override
    public boolean add(Outbound outbound, Integer collateralId) {
        Outbound io = outbound.setCollateralId(collateralId);
        int i=outboundMapper.insert(io);
        return i>0?true:false;
    }



    //正常出库的分页查询
    @Transactional(readOnly = true)
    @Override
    public Page<Outbound> findPageZ(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Outbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据出库编号)
        LambdaQueryWrapper<Outbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Outbound::getStatus,0)
                .eq(Outbound::getType,0)
                .eq(StringUtils.hasLength(queryString),Outbound::getNum,queryString);
        //执行分页查询条件
        Page<Outbound> pageZ = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageZ;
    }


    //查询置换出库的信息
    @Transactional(readOnly = true)
    @Override
    public Page<Outbound> findPageR(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Outbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据出库编号)
        LambdaQueryWrapper<Outbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Outbound::getStatus,0)
                .eq(Outbound::getType,1)
                .eq(StringUtils.hasLength(queryString),Outbound::getNum,queryString);
        //执行分页查询条件
        Page<Outbound> pageR = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageR;
    }


    //查询临时出库的信息
    @Transactional(readOnly = true)
    @Override
    public Page<Outbound> findPageT(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Outbound> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据出库编号)
        LambdaQueryWrapper<Outbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Outbound::getType,2)
                .eq(StringUtils.hasLength(queryString),Outbound::getNum,queryString);
        //执行分页查询条件
        Page<Outbound> pageT = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageT;
    }



    //置换出库修改状态和结果
    @Override
    public boolean updateRS(Outbound outbound, Integer collateralId) {
        //先删除表中关系
        outboundMapper.delete(new LambdaQueryWrapper<Outbound>().eq(Outbound::getId , outbound.getId()));
        //在插入关系
        this.addConnectS(outbound,collateralId);
        //最后执行更新操作
        int b = outboundMapper.updateById(outbound);
        return b>0?true:false;
    }



    //修改部分的出库信息
    @Override
    public boolean updatePart(ZOutboundVo vo, Integer outboundId) {
        Outbound outbound = outboundMapper.selectById(outboundId);
        //同样点击同意后,将状态改为已出库，结果改为已通过
        outbound.setStatus(1);
        outbound.setResult(1);
        outbound.setApplyStaff(vo.getApplyStaff());
        outbound.setDescription(vo.getDescription());
        outbound.setAuditStaff(vo.getAuditStaff());
        outbound.setDate(vo.getDate());
        int i = outboundMapper.updateById(outbound);
        return i>0?true:false;

    }

    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Outbound> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Outbound::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }



    //根据申请出库传来的信息，来判断是怎样的出库类型
    @Override
    public int check(Integer collateralId) {
        LambdaQueryWrapper<Outbound>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        int type = 0;
        LambdaQueryWrapper<Outbound> eq = lambdaQueryWrapper.eq(Outbound::getCollateralId, collateralId);
        Outbound outbound = this.baseMapper.selectOne(eq);
        outbound.setStatus(1);
        type=outbound.getType();
        return type;
    }


    //根据id删除对应的出库消息
    @Override
    public boolean deleteMessByCid(Integer collId) {
        LambdaQueryWrapper<Outbound>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Outbound> eq = lambdaQueryWrapper.eq(Outbound::getCollateralId, collId);
        int b = this.baseMapper.delete(eq);
        return b>0?true:false;
    }


    //点击同意后实现，置换押品之间的关系
    public void addConnectS(Outbound o, Integer collateralId) {
        //Collateral collateral = new Collateral();
        Outbound outbound = o.setCollateralId(collateralId);
        //置换操作改变状态和结果
        outbound.setStatus(1);
        outbound.setResult(1);
        outboundMapper.insert(outbound);
    }



    //

}
